package com.chushouya.aop;

import com.chushouya.common.support.DevOption;
import com.chushouya.common.support.RequestHeaders;
import com.chushouya.common.support.RequestUserContext;
import com.chushouya.manager.constants.UserTypeEnum;
import com.chushouya.manager.dto.admin.login.WebAppUserAuthDTO;
import com.chushouya.manager.service.api.WebAppTokenService;
import com.general.framework.core.enums.ContextUserType;
import com.general.framework.core.exception.CodeEnum;
import com.general.framework.core.exception.Ex;
import com.general.framework.core.lang.Strings;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * Admin认证拦截器
 * @author leejean team
 */
@Slf4j
@Aspect
@Component
@Order(200)
public class WebAppAuthAspect extends BaseAuthAspect<WebAppUserAuthDTO>{

    @Resource
    private HttpServletRequest request;

    @Resource
    private WebAppTokenService webAppTokenService;

    /**
     * 用户认证处理
     */
    @Around("execution(* com.chushouya.controller.*.api.*.*(..)) || execution(* com.chushouya.controller.*.clerk.*.*(..))")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        // 免登陆
        if (super.skipAuthValidate(pjp)) {
            return pjp.proceed();
        }
        final String token = request.getHeader(RequestHeaders.TOKEN);
        if (DevOption.isDevModel() && Strings.isBlank(token)) {
            super.setDevUserContext();
            return pjp.proceed();
        }
        if (Strings.isBlank(token)) {
            throw Ex.business(CodeEnum.UNAUTHORIZED);
        }
        try {
            final WebAppUserAuthDTO adminUserAuth = webAppTokenService.authToken(token);
//            log.info("WebAppAuthAspect.doAround: userAuth={}", adminUserAuth);
            super.setUserContext(adminUserAuth);
            return pjp.proceed();
        } finally {
            super.clearUserContext();
        }
    }

    @Override
    protected RequestUserContext buildUserContext(WebAppUserAuthDTO userAuth) {
        if (Objects.isNull(userAuth)) {
            return null;
        }
        final RequestUserContext userContext = new RequestUserContext();
        userContext.setUserId(userAuth.getUserId());
        userContext.setUserName(userAuth.getUserName());
        // 设置Context用户类型
        final Integer userType = userAuth.getUserType();
        if (Objects.equals(userType, UserTypeEnum.normal_user.value())) {
            userContext.setUserType(ContextUserType.APP_USER.value());
        } else if (Objects.equals(userType, UserTypeEnum.merchant_manager.value())) {
            userContext.setUserType(ContextUserType.APP_MERCHANT_MANAGER.value());
            userContext.setMerchantId(userAuth.getMerchantId());
        } else if (Objects.equals(userType, UserTypeEnum.merchant_quotation.value())) {
            userContext.setUserType(ContextUserType.APP_MERCHANT_QUOTATION.value());
            userContext.setMerchantId(userAuth.getMerchantId());
        } else if (Objects.equals(userType, UserTypeEnum.store_manager.value())) {
            userContext.setUserType(ContextUserType.APP_STORE_MANAGER.value());
            userContext.setStoreId(userAuth.getStoreId());
        } else if (Objects.equals(userType, UserTypeEnum.store_employee.value())) {
            userContext.setUserType(ContextUserType.APP_STORE_EMPLOYEE.value());
            userContext.setStoreId(userAuth.getStoreId());
        } else if (Objects.equals(userType, UserTypeEnum.clerk.value())) {
            userContext.setUserType(ContextUserType.APP_CLERK.value());
            userContext.setClerkId(userAuth.getClerkId());
        }
        return userContext;
    }

}
